package gui.layer;

import java.io.IOException;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 * Class for the arrows.
 * @author Sandra Mayer
 *
 */
public class ArrowTiledLayer extends BaseLayer {
	
	private static final int POSOFARROW1 = 0;
	private static final int POSOFARROW2 = 2;
	private static final int POSOFARROW3 = 4;
	private static final int POSOFARROW4 = 14;
	private static final int POSOFARROW5 = 24;
	private static final int POSOFARROW6 = 22;
	private static final int POSOFARROW7 = 20;
	private static final int POSOFARROW8 = 10;

	public final static String FILENAME = "/arrow.PNG";
	private Graphics m_Graphics = null;
	int[] curField = null;

	/**
	 * Constructor.
	 * @param g Graphics
	 * @throws IOException IOException
	 */
	public ArrowTiledLayer(Graphics g) throws IOException {
		super(BaseLayer.NROFCELLSHOR,
				BaseLayer.NROFCELLSVER, FILENAME,
				BaseLayer.SIZEOFCELL, BaseLayer.SIZEOFCELL);
		m_Graphics = g;
	}
	
	/**
	 * This method updates the text for the arrows. The texts says how many 
	 * rabbits are in the direction the arrow points.
	 * @param nrOfArrow the number of the arrow
	 * 		1  2  3
	 *      8     4
	 *      7  6  5
	 * @param amountOfRabbits The amount of rabbits that is in the given direction
	 */
	public void updateGraphics(int nrOfArrow, int amountOfRabbits) {
		if (amountOfRabbits != 0) {
			if (nrOfArrow > 0 || nrOfArrow < 9) {
				
				m_Graphics.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
				m_Graphics.setColor(0xFFFF00);
				switch (nrOfArrow) {
				case 1:
					if (curField[POSOFARROW1] == 1)
						m_Graphics.drawString(String.valueOf(amountOfRabbits), 3, 20, Graphics.BOTTOM|Graphics.LEFT);
					break;
				case 2:
					if (curField[POSOFARROW2] == 2)
						m_Graphics.drawString(String.valueOf(amountOfRabbits), 113, 20, Graphics.BOTTOM|Graphics.LEFT);
					break;
				case 3:
					if (curField[POSOFARROW3] == 3)
						m_Graphics.drawString(String.valueOf(amountOfRabbits), 228, 20, Graphics.BOTTOM|Graphics.LEFT);
					break;
				case 4:
					if (curField[POSOFARROW4] == 4)
						m_Graphics.drawString(String.valueOf(amountOfRabbits), 228, 128, Graphics.BOTTOM|Graphics.LEFT);
					break;
				case 5:
					if (curField[POSOFARROW5] == 5)
						m_Graphics.drawString(String.valueOf(amountOfRabbits), 228, 218, Graphics.TOP|Graphics.LEFT);
					break;
				case 6:
					if (curField[POSOFARROW6] == 6)
						m_Graphics.drawString(String.valueOf(amountOfRabbits), 113, 218, Graphics.TOP|Graphics.LEFT);
					break;
				case 7:
					if (curField[POSOFARROW7] == 7)
						m_Graphics.drawString(String.valueOf(amountOfRabbits), 3, 218, Graphics.TOP|Graphics.LEFT);
					break;
				case 8:
					if (curField[POSOFARROW8] == 8)
						m_Graphics.drawString(String.valueOf(amountOfRabbits), 3, 128, Graphics.BOTTOM|Graphics.LEFT);
					break;
				default:
					break;
				}
			}
		}
	}
	
	/**
	 * This method updates the gamefield with the new positions of arrows.
	 * @param posOfArrows the positions of the arrows
	 */
	public void update(int[] posOfArrows) {
		curField = posOfArrows;
		// set the background with the images
		for (int i = 0; i < posOfArrows.length; i++) {
		    int column = i % NROFCELLSHOR;
		    int row = (i - column)/NROFCELLSHOR;
		    this.setCell(column, row, posOfArrows[i]);
		}
		// set the location of the background
		this.setPosition(GAME_ORIGIN_X, GAME_ORIGIN_Y); 
	}
}
